/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.web;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.Role;
import com.thinkgem.jeesite.modules.sys.entity.TbUserVerify;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.service.TbUserVerifyService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 身份证审核Controller
 * @author huoyan403
 * @version 2016-10-29
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/tbUserVerify")
public class TbUserVerifyController extends BaseController {

	@Autowired
	private TbUserVerifyService tbUserVerifyService;
	@Autowired
	private SystemService systemService;
	
	@ModelAttribute
	public TbUserVerify get(@RequestParam(required=false) String id) {
		TbUserVerify entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tbUserVerifyService.get(id);
		}
		if (entity == null){
			entity = new TbUserVerify();
		}
		return entity;
	}


	//@RequiresPermissions("sys:user:view")
	@RequestMapping(value = {"list", ""})
	public String list(TbUserVerify tbUserVerify, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user=UserUtils.getUser();
		if(null!=user){
			Role role=systemService.getRoleByUid(user);
			if("2".equals(role.getId())){
				tbUserVerify.setAddress(user.getProvinces()+user.getCitys());
			}
		}
		Page<TbUserVerify> page = tbUserVerifyService.findPage(new Page<TbUserVerify>(request, response), tbUserVerify); 
		model.addAttribute("page", page);
		model.addAttribute("tbuserverify",tbUserVerify);
		if(page.getList().size()==0){
			model.addAttribute("message1","无查询结果");
		}
		return "modules/sys/tbUserVerifyList";
	}

	//@RequiresPermissions("sys:user:view")
	@RequestMapping(value = "form")
	public String form(TbUserVerify tbUserVerify, Model model) {
		model.addAttribute("tbUserVerify", tbUserVerify);
		return "modules/sys/tbUserVerifyForm";
	}

	//@RequiresPermissions("sys:user:edit")
	@RequestMapping(value = "save")
	public String save(TbUserVerify tbUserVerify, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tbUserVerify)){
			return form(tbUserVerify, model);
		}
		tbUserVerifyService.save(tbUserVerify);
		addMessage(redirectAttributes, "保存身份证审核成功");
		return "redirect:"+Global.getAdminPath()+"/sys/tbUserVerify/?repage";
	}

	//@RequiresPermissions("sys:user:edit")
	@RequestMapping(value = "edit",method = RequestMethod.POST)
	@ResponseBody
	public TbUserVerify delete(TbUserVerify tbUserVerify,HttpServletRequest request) {
		String id = request.getParameter("id");
		String auditorState = request.getParameter("auditorState");
		tbUserVerify.setId(id);
		tbUserVerify.setAuditorState(auditorState);
		tbUserVerifyService.edit(tbUserVerify);
		return tbUserVerify;
	}

}